<template>
  <div class="register-content">
    <h3 class="register__title">
      <img :src="logoIcon" alt="" />
      <span>{{ $t("login.register.loginInfo.title") }}</span>
    </h3>
    <div class="line"></div>
    <el-form
      label-width="110px"
      :rules="rules"
      :model="form"
      ref="ruleForm"
      @validate="validate"
    >
      <el-form-item :label="$t('label.mobile')" required prop="mobile">
        <el-input
          v-model="form.mobile"
          :placeholder="$t('placeholder.mobile')"
          class="input-with-select"
          size="large"
        >
          <template #prepend>
            <select-list-mobile-prefix v-model="select" />
          </template>
          <template #suffix>
            <i
              v-show="formIcon.mobile"
              :class="`iconfont ${formIcon.mobile}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('label.code')" required prop="code">
        <el-input
          v-model="form.code"
          :placeholder="$t('placeholder.code')"
          class="input-with-select"
          size="large"
          maxlength="6"
          @input="validateNum"
        >
          <template #append>
            <div
              class="code get-code"
              v-if="!codeEvent.flag"
              @click="
                codeEvent.getCode({
                  phoneNumber: form.mobile,
                  type: 'captcha_register',
                })
              "
            >
              {{ $t("verCode.getCode") }}
            </div>
            <div class="code" v-else>
              {{ $t("verCode.resend") }}{{ codeEvent.time }}S
            </div>
          </template>
          <template #suffix>
            <i v-show="formIcon.code" :class="`iconfont ${formIcon.code}`"></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('label.password')" required prop="password">
        <el-input
          v-model="form.password"
          :placeholder="$t('login.register.loginInfo.password.placeholder')"
          class="input-with-select"
          size="large"
          type="password"
          show-password
        >
          <template #suffix>
            <i
              v-show="formIcon.password"
              :class="`iconfont ${formIcon.password}`"
            ></i>
          </template>
        </el-input>
        <p class="tips">{{ $t("login.register.loginInfo.password.tips") }}</p>
      </el-form-item>
      <el-form-item
        :label="$t('label.confirmPassword')"
        required
        prop="confirmPassword"
      >
        <el-input
          v-model="form.confirmPassword"
          :placeholder="$t('placeholder.confirmPassword')"
          class="input-with-select"
          size="large"
          type="password"
          show-password
        >
          <template #suffix>
            <i
              v-show="formIcon.confirmPassword"
              :class="`iconfont ${formIcon.confirmPassword}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item :label="$t('label.userName')" required prop="userName">
        <el-input
          v-model="form.userName"
          :placeholder="$t('placeholder.userName')"
          class="input-with-select"
          size="large"
        >
          <template #suffix>
            <i
              v-show="formIcon.userName"
              :class="`iconfont ${formIcon.userName}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('label.email')" prop="email">
        <el-input
          v-model="form.email"
          :placeholder="$t('placeholder.email')"
          class="input-with-select"
          size="large"
        >
          <template #suffix>
            <i
              v-show="formIcon.email"
              :class="`iconfont ${formIcon.email}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('label.unitType')" required prop="unitType">
        <select-list-unit v-model="form.unitType" />
      </el-form-item>
      <el-form-item :label="$t('label.unitName')" required prop="unitName">
        <el-input
          v-model="form.unitName"
          :placeholder="$t('placeholder.unitName')"
          class="input-with-select"
          size="large"
        >
          <template #suffix>
            <i
              v-show="formIcon.unitName"
              :class="`iconfont ${formIcon.unitName}`"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('label.profession')" required prop="profession">
        <select-list-profession v-model="form.profession" />
      </el-form-item>
      <el-form-item label="">
        <div
          class="register__btn"
          :class="{ 'register__btn--active': submitBtn }"
          @click="submit"
        >
          {{ $t("login.register.nextStep") }}
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import logoIcon from "@/assets/images/logo_icon.png";
import {
  SelectListMobilePrefix,
  SelectListUnit,
  SelectListProfession,
} from "@/components/select-banner";
import {
  validateMobile,
  validatePassword,
  validateCode,
  validateuserName,
  validateEmail,
} from "@/utils/regular";
import verificationCode from "@/composables/verificationCode";
const { codeEvent } = verificationCode();
</script>
<script>
import { dataMiddleGroundBaseUserRegisterUser } from "@/api/data-middleground-base/base/user";
export default {
  props: {
    mobileInfo: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      form: {
        mobile: "",
        code: "",
        password: "",
        confirmPassword: "",
        userName: "",
        email: "",
        unitType: "",
        unitName: "",
        profession: "",
      },
      formIcon: {
        mobile: "",
        code: "",
        password: "",
        confirmPassword: "",
        userName: "",
        email: "",
        unitType: "",
        unitName: "",
        profession: "",
      },
      select: "",
      code: {
        time: 60,
        text: "获取验证码",
      },
      rules: {},
    };
  },
  computed: {
    submitBtn() {
      let arr = Object.values(this.formIcon);
      return arr.every((item) => item == "ic-circle-correct-fill");
    },
  },
  created() {
    this.setRules();
  },
  mounted() {
    this.init();
  },
  methods: {
    //数据初始化
    init() {
      this.form.mobile = this.mobileInfo.number;
      this.select = this.mobileInfo.prefix;
    },
    //校验规则
    setRules() {
      this.rules = {
        mobile: [
          {
            trigger: "change",
            validator: validateMobile,
          },
        ],
        code: [
          {
            trigger: "change",
            validator: validateCode,
          },
          {
            trigger: "blur",
            validator: validateCode,
          },
        ],
        password: [
          {
            trigger: "change",
            validator: validatePassword,
          },
          {
            trigger: "blur",
            validator: validatePassword,
          },
        ],
        confirmPassword: [
          {
            trigger: "change",
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(this.$t("validate.confirmPassword.empty"));
              } else {
                if (this.form.password != this.form.confirmPassword) {
                  callback(this.$t("validate.confirmPassword.error"));
                } else {
                  callback();
                }
              }
            },
          },
        ],
        userName: [
          {
            trigger: "blur",
            validator: validateuserName,
            required: true,
          },
        ],
        email: [
          {
            trigger: "blur",
            validator: validateEmail,
          },
        ],
        unitType: [
          {
            trigger: "change",
            message: this.$t("validate.unitType.empty"),
            required: true,
          },
        ],
        unitName: [
          {
            trigger: "blur",
            message: this.$t("validate.unitName.empty"),
            required: true,
          },
        ],
        profession: [
          {
            trigger: "change",
            message: this.$t("validate.profession.empty"),
            required: true,
          },
        ],
      };
    },
    //数字校验
    validateNum() {
      const reg = /\d/g;
      if (!reg.test(this.form.code)) {
        this.form.code = "";
      }
    },
    //每次验证icon
    validate(key, flag) {
      if (flag) {
        this.formIcon[key] = "ic-circle-correct-fill";
      } else {
        this.formIcon[key] = "ic-circle-wrong-fill";
      }
    },
    //下一步
    submit() {
      this.$refs.ruleForm.validate(async (flag, object) => {
        if (flag) {
          let obj = JSON.parse(JSON.stringify(this.form));
          delete obj.confirmPassword;
          const res = await dataMiddleGroundBaseUserRegisterUser(obj);
          if (res.errno == 0) {
            ElMessage({
              message: this.$t(`responseMsg.${res.errno}`),
              type: "success",
            });
            this.$emit("nextStep", "request-success");
          }
        } else {
          ElMessage({
            message: this.$t("validate.submit.error"),
            type: "warning",
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.register-content {
  width: 100%;
  height: 100%;
  padding: 56px 64px;
}
.register {
  &__title {
    font-size: 32px;
    margin-bottom: 24px;
    @include flex-y-center;
    img {
      margin-right: 16px;
      width: 40px;
      height: 40px;
    }
  }
  .line {
    width: 100%;
    height: 1px;
    background: map-get($map: $fill-color, $key: deep);
    margin: 35px 0;
  }

  &__btn {
    width: 400px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 2px;
    color: map-get($font-sizes, white);
    background: map-get($bg-primary, disabled);
  }
  &__btn--active {
    background: map-get($bg-primary, active);
    cursor: pointer;
  }
  &__forget-pwd {
    width: 100%;
    color: map-get($font-sizes, smTitle);
    text-align: center;
    @include absolute-left-bottom-0();
    bottom: 40px;
  }
}
:deep(.el-form-item) {
  --el-color-white: #f2f3f5;
  margin-bottom: 16px;
  .el-form-item__label {
    line-height: 40px;
  }
  .el-form-item__content {
    flex: inherit;
    width: 400px;
  }
  .el-input__inner {
    border: none;
    color: map-get($font-sizes, title);
  }
  .el-input-group__prepend {
    border: none;
    --el-bg-color: rgba(242, 243, 245, 1);
    position: relative;
    &::after {
      display: inline-block;
      content: "";
      width: 1px;
      height: 22px;
      position: absolute;
      right: 0;
      top: 50%;
      margin-top: -11px;
      background: map-get($map: $fill-color, $key: deep);
    }
    .el-input__inner {
      padding: 0 0.2rem;
    }
  }
  .el-input__prefix {
    left: 12px;
  }
  i {
    height: 40px;
    line-height: 40px;
    color: map-get($font-sizes, text);
  }
  .el-input__validateIcon {
    display: none;
  }
  .ic-circle-correct-fill {
    color: map-get($map: $status-success, $key: static);
  }
  .ic-circle-wrong-fill {
    color: #f53f3f;
  }
  .tips {
    font-size: 12px;
    color: map-get($map: $font-sizes, $key: smText);
  }
  .iconfont {
    margin-right: 5px;
  }
  input[type="password"]::-ms-reveal {
    display: none;
  }
  .el-input-group__append {
    border: none;
    position: relative;
    --el-bg-color: rgba(242, 243, 245, 1);
    color: map-get($map: $font-sizes, $key: smText);
    &::before {
      content: "";
      display: inline-block;
      width: 1px;
      height: 20px;
      background: map-get($map: $stroke-color, $key: weight);
      position: absolute;
      top: 50%;
      margin-top: -10px;
      left: 0;
    }
    .code-text {
      min-width: 84px;
    }
    .get-code {
      cursor: pointer;
    }
  }
}
</style>
